﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using EIA.PM.Model;
using EIA.PM.DAL;

namespace EIA.PM.BLL
{
    public class TRightBlp : ITRightBlp
    {
        ITRightDao _TRightDao = null;
        public TRightBlp()
        {
            _TRightDao = X.DaoFactory.Create<ITRightDao>();
        }

        public int Insert(TRight o)
        {
            object r = _TRightDao.Insert(o);
            return r is int ? (int)r : 0;
        }

        public IList<int> InsertList(IList<TRight> lst)
        {
            _TRightDao.DASession.BeginTransaction();
            try
            {
                IList<int> keys = new List<int>();
                foreach (TRight o in lst)
                {
                    object r = _TRightDao.Insert(o);
                    keys.Add(r is int ? (int)r : 0);
                }
                _TRightDao.DASession.CommitTransaction();
                return keys;
            }
            catch
            {
                _TRightDao.DASession.RollbackTransaction();
                throw;
            }
        }

        public bool Update(TRight o)
        {
            return _TRightDao.Update(o);
        }

        public bool UpdateChanged(IDictionary props)
        {
            return _TRightDao.Update(props);
        }

        public bool UpdateList(IList<TRight> lst)
        {
            _TRightDao.DASession.BeginTransaction();
            try
            {
                foreach (TRight o in lst)
                {
                    if (!_TRightDao.Update(o))
                    {
                        _TRightDao.DASession.RollbackTransaction();
                        return false;
                    }
                }
                _TRightDao.DASession.CommitTransaction();
                return true;
            }
            catch
            {
                _TRightDao.DASession.RollbackTransaction();
                throw;
            }
        }

        public bool Delete(int o_id)
        {
            return _TRightDao.Delete(o_id) > 0;
        }

        public bool DeleteList(IList<TRight> lst)
        {
            _TRightDao.DASession.BeginTransaction();
            try
            {
                foreach (TRight o in lst)
                {
                    if (_TRightDao.Delete(o) != 1)
                    {
                        _TRightDao.DASession.RollbackTransaction();
                        return false;
                    }
                }
                _TRightDao.DASession.CommitTransaction();
                return true;
            }
            catch
            {
                _TRightDao.DASession.RollbackTransaction();
                throw;
            }
        }

        public int Count(IDictionary con)
        {
            return _TRightDao.Count(con);
        }

        public IList<TRight> List(IDictionary con)
        {
            return _TRightDao.List(con);
        }

        public IList<TRight> ListAll()
        {
            return _TRightDao.List(null);
        }
    }
}
